/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "MudExit.h"
#include "CmdVisitor.h"
#include "MudPlayer.h"
#include "Area.h"
#include "World.h"
#include "Global.h"

using namespace Chronos;
using namespace std;

RoomPortal::RoomPortal(Area& area_
			, int destRoomID_
			, const std::string& arriveMsg_
			, const std::string& leaveMsg_
			, const std::string& enterMsg_)
	: area(area_)
	, destRoomID(destRoomID_)
	, arriveMsg(arriveMsg_)
	, leaveMsg(leaveMsg_)
	, enterMsg(enterMsg_)
{}


void RoomPortal::pass(MudPlayer& player) {
    if (enterMsg != "") player.write(enterMsg + "\n");
    area.movePlayer(player, destRoomID, player.getName() + leaveMsg, player.getName() + arriveMsg);
}


AreaPortal::AreaPortal(int destAreaID_
			, int destRoomID_
			, const std::string& arriveMsg_
			, const std::string& leaveMsg_
			, const std::string& enterMsg_)
	: destAreaID(destAreaID_)
	, destRoomID(destRoomID_)
	, arriveMsg(arriveMsg_)
	, leaveMsg(leaveMsg_)
	, enterMsg(enterMsg_)
{}


void AreaPortal::pass(MudPlayer& player) {
    if (enterMsg != "") player.write(enterMsg + "\n");
	Global<World>()->movePlayer(player, destAreaID, destRoomID, player.getName() + arriveMsg, player.getName() + leaveMsg);
}


MudExit::MudExit(const string& name_, const string& descr_, Portal* portal_)
    : MudObject(name_, descr_)
	, portal(portal_)
{}

MudExit::~MudExit() {}

VISITOR_ACCEPT(MudExit)

void MudExit::pass(MudPlayer& player) {
    if (!canPass(player)) return;
    player.generateEvent(MUDMOBILE_MOVEMENT);
	portal->pass(player);
}

bool MudExit::canPass(MudPlayer& player) {
	return !activateTrigger(MUDEXIT_PASS, (MudMobile*) &player);
}


MudRoomExit::MudRoomExit(const std::string& name_
			, const std::string& descr_
			, const std::string& arriveMsg_
			, const std::string& leaveMsg_
			, const std::string& enterMsg_
			, Area& area_
			, unsigned int destRoomID_)
	: MudExit(name_, descr_, new RoomPortal(area_, destRoomID_, arriveMsg_, leaveMsg_, enterMsg_))
{}

MudNorthExit::MudNorthExit(Area& area_, int destRoomID_)
	: MudExit("north", "the exit north.", new RoomPortal(area_, destRoomID_, " arrives from the south.", " leaves north.", ""))
{}

MudEastExit::MudEastExit(Area& area_, int destRoomID_)
	: MudExit("east", "the exit east.", new RoomPortal(area_, destRoomID_, " arrives from the west.", " leaves east.", ""))
{}

MudSouthExit::MudSouthExit(Area& area_, int destRoomID_)
	: MudExit("south", "the exit south.", new RoomPortal(area_, destRoomID_, " arrives from the north.", " leaves south.", ""))
{}

MudWestExit::MudWestExit(Area& area_, int destRoomID_)
	: MudExit("west", "the exit west.", new RoomPortal(area_, destRoomID_, " arrives from the east.", " leaves west.",  ""))
{}

MudUpExit::MudUpExit(Area& area_, int destRoomID_)
	: MudExit("up", "the exit up.", new RoomPortal(area_, destRoomID_, " arrives from below.", " leaves up.", ""))
{}

MudDownExit::MudDownExit(Area& area_, int destRoomID_)
	: MudExit("down", "the exit down.", new RoomPortal(area_, destRoomID_, " arrives from above.", " leaves down.", ""))
{}

